package io.adrainty.algorithm.leetcode.lcr;

import java.util.Arrays;

/**
 * LCR 014. 字符串的排列
 * @link <a href="https://leetcode.cn/problems/MPnaiL/description/">...</a>
 * @author AdRainty
 * @version V1.0.0
 * @since 2024/9/8 下午10:17
 */
@SuppressWarnings("unused")
public class LCR014 {

    private LCR014() {}

    public static boolean checkInclusion(String s1, String s2) {
        if (s2.length() < s1.length()) return false;

        int[] cnt1 = new int[26];
        int[] cnt2 = new int[26];

        for (int i = 0; i < s1.length(); i++) {
            cnt1[s1.charAt(i) - 'a']++;
            cnt2[s2.charAt(i) - 'a']++;
        }

        if (Arrays.equals(cnt1, cnt2)) return true;
        for (int i = 0; i < s2.length() - s1.length(); i++) {
            cnt2[s2.charAt(i) - 'a']--;
            cnt2[s2.charAt(i + s1.length()) - 'a']++;
            if (Arrays.equals(cnt1, cnt2)) return true;
        }
        return false;
    }

}
